/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package famtime.tm.kalender.presentation;

import famtime.tm.kalender.business.KalenderService;
import famtime.tm.kalender.model.Event;
import famtime.tm.kalender.model.FamUser;
import famtime.tm.kalender.model.Familie;
import famtime.tm.kalender.model.Kalender;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author Marius
 */
@Named
@SessionScoped
public class KalenderPM implements Serializable {
    
    @EJB
    private KalenderService service;
    private String username, password;
    private String familienName;
    private FamUser user;
    private Date actualDateBiatch, startTime, endTime;
    private String titel, description;  //Event Datenfelder
    private long id; // Event löschen
    
    private String eTitle, eDescription;
    private Date eStartTime, eEndTime;
 

    public String geteTitle() {
        return eTitle;
    }

    public void seteTitle(String eTitle) {
        this.eTitle = eTitle;
    }

    public String geteDescription() {
        return eDescription;
    }

    public void seteDescription(String eDescription) {
        this.eDescription = eDescription;
    }

    public Date geteStartTime() {
        return eStartTime;
    }

    public void seteStartTime(Date eStartTime) {
        this.eStartTime = eStartTime;
    }

    public Date geteEndTime() {
        return eEndTime;
    }

    public void seteEndTime(Date eEndTime) {
        this.eEndTime = eEndTime;
    }

    private List<FamUser> familieMembers;

    public List<FamUser> getFamilieMembers() {
        return familieMembers;
    }

    public void setFamilieMembers(List<FamUser> familieMembers) {
        this.familieMembers = familieMembers;
    }

    public boolean[] getActiveArray() {
        return activeArray;
    }

    public void setActiveArray(boolean[] activeArray) {
        this.activeArray = activeArray;
    }
    private boolean[] activeArray;
    
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public void setTitel(String titel) {
        this.titel = titel;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getStartTime() {
        return startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public String getTitel() {
        return titel;
    }

    public String getDescription() {
        return description;
    }

    /**
     *
     * @throws IOException
     */
    @PostConstruct
    private void init() throws IOException{
        if(!isLoggedIn()) {

        }
        else
        {
            actualDateBiatch = new Date();
            this.startTime = null;
        }
    }
    
    public String[] getDayArray(){
        String[] array = new String[7];
        Date currentDate = new Date();
        for(int i = 0; i < 7; i++){
            array[i] = getDayName(currentDate.getDay()) + " " + currentDate.getDate() + "." + (currentDate.getMonth() + 1) + ".";
            currentDate.setDate(currentDate.getDate()+1);
        }
        
        return array;
    }
    
    private String getDayName(int i){
        switch(i){
            case 1:
                return "Montag";
            case 2: 
                return "Dienstag";
            case 3: 
                return "Mittwoch";
            case 4:
                return "Donnerstag";
            case 5: 
                return "Freitag";
            case 6: 
                return "Samstag";
            case 0:
                return "Sonntag";
            default :
                return "";
        }
    }
    
    public void addKalender() {
        Kalender kalender = new Kalender(user);
        kalender.setUser(user);
        user.setKalender(kalender);
        service.create(kalender);
        user.setKalender(kalender);

    }
    
    //Getter+Setter
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFamilienName() {
        return familienName;
    }

    public void setFamilienName(String familienName) {
        this.familienName = familienName;
    }
    public String getPassword() {
        return password;
    }

    public void setService(KalenderService service) {
        this.service = service;
    }

    public void setUser(FamUser user) {
        this.user = user;
    }

    public void setActualDateBiatch(Date actualDateBiatch) {
        this.actualDateBiatch = actualDateBiatch;
    }

    public KalenderService getService() {
        return service;
    }

    public FamUser getUser() {
        return user;
    }
 
    public List<Event> findEventsByKalender()
    {
        return service.findEventsInKalender(user.getKalender());  
    }
    
    public List<FamUser> findUserByFamilie()
    {
        return service.findUserInFamilie(user.getParent());
    }
    
    public List<Kalender> findKalenderByFamilie()
    {
        return service.findKalenderInFamilie(findUserByFamilie());
    }
    
    public List<Event> findEventsByFamilie()
    {
        return service.findEventsInFamilie(findKalenderByFamilie());
    }
    
    public Date getActualDateBiatch() {
        return actualDateBiatch;
    }
    
    
    public void login() throws IOException{
        user = service.login(username,password);
        this.username = "";
        this.password = "";
        if(user != null) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        }
        this.actualDateBiatch = new Date();
        
        System.out.println("OMQ ITS A TAP");
        this.familieMembers = findUserByFamilie();
        this.activeArray = new boolean[familieMembers.size()];
        initActiveArray();
    }
    
    
    public void register() throws IOException{
        FamUser newUser = new FamUser(username, password);
        service.register(newUser);

        Kalender kalender = new Kalender(newUser);
        kalender.setUser(newUser);
        newUser.setKalender(kalender);
        service.create(kalender);
        service.addUserToFamilie(newUser, familienName);

        login();
    }
    
    public void registerFamilie() throws IOException{
        Familie familie = new Familie(familienName);
        
        service.register(familie);
        FacesContext.getCurrentInstance().getExternalContext().redirect("register.xhtml");
    }
    
    public void logout() throws IOException{
        this.user = null;
        FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
    }
    
    public boolean isLoggedIn() {
       return this.user != null;
    }

    /**
     * Event erstellen in den service ausgelagert.
     * funzt jetzt
     */
    public void addEventTest(){
        Date date = new Date(113,0,1,13,37,00);

        service.addEvent("TestTitel", "TestDescription", date, date, user.getKalender());

        System.out.println("TESTEVENT EINGEFÜGT");        
    }    
   
    public void addEvent(){
        service.addEvent1(titel,description,startTime,endTime,user.getKalender());
        titel=null;
        description=null;
        startTime=null;
        endTime=null;
    }
    
    public void addEvent2(){
        service.addEvent1(eTitle,eDescription,eStartTime,eEndTime,user.getKalender());
        eTitle=null;
        eDescription=null;
        eStartTime=null;
        eEndTime=null;
    }
        
    public void editEvent() throws IOException{
        addEvent2();
        removeEvent();
        FacesContext.getCurrentInstance().getExternalContext().redirect("close.xhtml");
    }
        
    public void removeEvent() throws IOException{
        service.removeEvent(id, user.getKalender());
        FacesContext.getCurrentInstance().getExternalContext().redirect("close.xhtml");
    }
    
    public void removeEvent(String id){
        long ID = Long.parseLong(id);
        service.removeEvent(ID, user.getKalender()); 
    }
    
    public String gebIdAus(String test){
        int i = Integer.parseInt(test);
        System.out.println(i + "BLABLA");
        return test;
    }
    
    public Event findEventByID(String id){  

            long eventID = Long.parseLong(id); 
        
            for(Event e : findEventsByFamilie()) {
            if(e.getId() == eventID) {
                return e;
            }
        }
        return null;
    }
           
    
    /**
     * YEAAAAA
     * @return 
     */
    public List<Event>[][] getkalenderArray()
    {
        
        ArrayList<Event>[][] kalenderArray;
        kalenderArray = new ArrayList[24][8];
        
        for(int i = -1; i < 7; i++)
        {
            for(int j = 0; j < 24; j++)
            {
             kalenderArray[j][i+1] = (ArrayList<Event>) findEventsForHour(i,j);
            }
        }
     return kalenderArray;
    }
        
     /**
     * Precoditino: hour is a value between 0 and 23
     * @param dayOffset
     * @param hour
     * @return
     */
    public List<Event> findEventsForHour(int dayOffset, int hour) {

        Date date = new Date();
        

        date.setDate(date.getDate() + dayOffset);

        List<Event> allEvents = findEventsByFamilie();
        List<Event> hourEvents = new ArrayList<Event>();
        if(dayOffset == -1){
            date.setHours(hour);
            hourEvents.add(new Event("test666","test",date,date));
            return hourEvents;
        }
        for (Event e : allEvents) {
            //same day
            if (e.getStartDate().getYear() == date.getYear()
                    && e.getStartDate().getMonth() == date.getMonth()
                    && e.getStartDate().getDay() == date.getDay()
                    && e.getStartDate().getHours() == hour) {
                hourEvents.add(e);
            }
        }
        return hourEvents;
    }
    
    /**
     * 
     * @param i
     * @return 
     */
    public boolean isActive(FamUser u){
        return activeArray[findIndex(u)];
    }
    
    public int findIndex(FamUser u){
        return this.familieMembers.indexOf(u);
    }
    
    public String findUserClass(FamUser u){
        switch(this.familieMembers.indexOf(u)){
            case(0) :
                return "eins";
            case(1) : 
                return "zwei";
            case(2) : 
                return "drei";
            case(3) :
                return "vier";
            case(4)  : 
                return "fuenf";
            default :
                return "toomuch";
        }
    }


    private void initActiveArray() {
        for(int i = 0; i < activeArray.length; i++)
        {
          activeArray[i] = true;
        }
    }
    
    public void initB(Event e)
    {
        
        this.eDescription = e.getDescription();
        this.eTitle = e.getTitel();
        this.eStartTime = e.getStartDate();
        this.eEndTime = e.getEndDate();
        this.id = e.getId();
    }
    
    public boolean notSubmitted(String s)
    {
        return s.length() > 0;
    }
    
    
}
